package com.taurus.myboard.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.taurus.myboard.common.vo.Dict;
import com.taurus.myboard.service.IJobManageService;
import com.taurus.myboard.utils.HttpClientPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class JobManageServiceImpl implements IJobManageService {

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private HttpClientPool httpClientPool;

    @Value("${fateflow.url}")
    private String url;

    public Map<String, String> getJob(String jobId, String role, String partyId){
        Map<String, String> jobParams = new HashMap<>();
        jobParams.put(Dict.JOBID, jobId);
        jobParams.put(Dict.ROLE, role);
        jobParams.put(Dict.PARTY_ID, partyId);
        String params = JSON.toJSONString(jobParams);
        //restTemplate 会根据 params 的具体类型，调用合适的 HttpMessageConvert 将请求参数写到请求体 body 中，并在请求头中添加合适的 content-type；
        // 也会根据 responseType 的类型（本列子中是 JSONObject），设置 head 中的 accept 字段，当响应返回的时候再调用合适的 HttpMessageConvert 进行响应转换
//        ResponseEntity<String> responseEntity=restTemplate.postForEntity(url, params, String.class);
        Map<String, String> result = new HashMap<>();
        String rst = httpClientPool.post(url + Dict.URL_JOB_DATAVIEW, JSON.toJSONString(jobParams));
        JSONObject data = JSON.parseObject(rst).getJSONObject(Dict.DATA);
//        int statusCodeValue = responseEntity.getStatusCodeValue();
//        HttpHeaders headers = responseEntity.getHeaders();
//        JSONObject body = responseEntityponseEntity.getBody();
        result.put("job", rst);
        return result;
    }
}
